<template>
  <div class="login_container">
    <div class="login_box">
      <!--头像区-->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt=""/>
      </div>
      <!--登录表单区-->
      <el-form label-width="0" class="login_form"
               :model="loginForm"
               ref="loginFormRef" :rules="loginFormRules">
        <!--用户名-->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username"
                    prefix-icon="el-icon-user-solid"
                    placeholder="用户名"></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password"
                    show-password
                    prefix-icon="el-icon-lock"
                    placeholder="密码"></el-input>
        </el-form-item>
        <!--按钮区域-->
        <el-form-item class="btns">
          <el-button
            type="primary"
            @click="login('loginFormRef')">
            登录
          </el-button>
          <el-button
            type="info"
            @click="resetLoginForm('loginFormRef')">
            重置
          </el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        //登录表单的绑定数据
        loginForm: {
          username: 'admin',
          password: '123456'
        },
        // 表单的验证规则
        loginFormRules: {
          username: [
            {
              required: true,
              message: '请输入用户名',
              trigger: 'blur'
            },
            {
              min: 2,
              max: 10,
              message: '长度在2-10之间',
              trigger: 'blur'
            }
          ],
          password: [
            {
              required: true,
              message: '请输入密码',
              trigger: 'blur'
            },
            {
              min: 6,
              max: 16,
              message: '长度在6-16之间',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      //点击重置按钮，重置登录表单
      resetLoginForm (refName) {
        this.$refs[refName].resetFields()
      },
      // 登录请求前表单预验证，根据valid的布尔值判断是否发情请求
      login (refName) {
        this.$refs[refName].validate(async valid => {
          if (!valid) return
          const { data: res } = await this.$http.post('/login', this.loginForm)
          if (res.meta.status !== 200) return this.$message.error('登录失败:' + res.msg)
          this.$message({
            showClose: true,
            message: '登录成功',
            type: 'success',
            duration: 1500
          })
          // 项目中除登录接口可以访问之外，其他都要验证，token存在sessionStorage中，只在当前网站打开期间有效
          window.sessionStorage.setItem('token', res.data.token)
          await this.$router.push('/home')
        })
      }
    }
  }
</script>

<style lang="stylus" scoped>//scoped代表样式只作用于当前组件
.login_container {
  background-color #2b4b6b
  height 100%

  .login_box {
    width 450px
    height 300px
    background-color #FFF
    border-radius 3px
    position absolute
    left 50%
    top 50%
    transform translate(-50%, -50%)

    .avatar_box {
      height 130px
      width 130px
      border 1px solid #EEE
      border-radius 50%
      padding 10px
      box-shadow 0 0 10px #ddd
      position absolute
      left 50%
      transform translate(-50%, -50%)
      background-color #FFF

      img {
        width 100%
        height 100%
        border-radius 50%
        background-color #EEE
      }
    }

    .login_form {
      position absolute
      width 100%
      bottom 0
      padding 0 20px
      box-sizing border-box


      .btns {
        display flex
        justify-content flex-end
      }
    }

  }


}
</style>
